package com.group3.service;

import com.group3.model.User;
import com.group3.model.RegisterRequest;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.stereotype.Service;

@Service
public interface UserService {

    // 定义资源和熔断降级处理方法
    @SentinelResource(value = "queryUserById",
            blockHandler = "blockHandlerQueryUserById",
            fallback = "fallbackQueryUserById")
    User queryUserById(Integer uid);

    // 定义资源和熔断降级处理方法
    @SentinelResource(value = "login",
            blockHandler = "blockHandlerLogin",
            fallback = "fallbackLogin")
    User login(String username, String password);

    // 注册接口熔断降级
    @SentinelResource(value = "registerUser",
            blockHandler = "blockHandlerRegisterUser",
            fallback = "fallbackRegisterUser")
    User registerUser(RegisterRequest request);

    // 熔断处理方法
    default User blockHandlerQueryUserById(Integer uid, BlockException ex) {
        return new User(); // 返回默认用户
    }

    // 降级处理方法
    default User fallbackQueryUserById(Integer uid, Throwable ex) {
        return new User(); // 返回默认用户
    }

    // 登录熔断处理方法
    default User blockHandlerLogin(String username, String password, BlockException ex) {
        throw new RuntimeException("登录服务暂时不可用，请稍后再试");
    }

    // 登录降级处理方法
    default User fallbackLogin(String username, String password, Throwable ex) {
        throw new RuntimeException("登录服务发生异常，请稍后再试");
    }

    // 注册熔断处理方法
    default User blockHandlerRegisterUser(RegisterRequest request, BlockException ex) {
        throw new RuntimeException("注册服务暂时不可用，请稍后再试");
    }

    // 注册降级处理方法
    default User fallbackRegisterUser(RegisterRequest request, Throwable ex) {
        throw new RuntimeException("注册服务发生异常，请稍后再试");
    }
}
